package com.example.bysj.Controller;
//201902104061徐中堂
import com.example.bysj.Service.ActorAssRoleService;
import com.example.bysj.Service.LoginService;
import com.example.bysj.Service.UserService;
import com.example.bysj.domain.User;
import com.example.bysj.domain.authority.Actor;
import com.example.bysj.domain.authority.ActorAssRole;
import com.example.bysj.domain.authority.LoginResult;
import com.example.bysj.domain.authority.Role;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.TreeSet;
//声明此类是一个Controller对象，返回指定文件形式
@Controller
@Slf4j
public class LoginController {
    //自动注入
    @Autowired
    private LoginService loginService;
    //指向login的view文件，String类型返回值指向模板文件
    @GetMapping(value = "/login")
    public String login(){
        return "login";
    }
    //返回登录界面
    @PostMapping(value = "/login")
    public String login(@RequestParam("username")String username,@RequestParam("password")String password){
        log.debug(username+"login()");
        //根据用户名和密码登录
        LoginResult loginResult = loginService.login(username,password);
        if (loginResult.isLogin()){
            log.debug("登陆成功");
            return "index";
        } else {
            throw new RuntimeException("登陆失败" + loginResult.getResult());
        }
    }
    //主页，退出登录
    @GetMapping(value = "index")
    public String index(){
        return "index";
    }

    @GetMapping(value = "logout")
    @ResponseBody
    public String logout(){
        String name = loginService.logout();
        return "用户" + name + "退出登录！";
    }

    //返回json格式
    @ResponseBody
    //该方法根据username和password登录，使用postman
    @GetMapping("/login.ctl")
    public String Plogin(@RequestParam("username")String username,@RequestParam("password")String password) {
        log.debug(username+"login()");
        LoginResult loginResult = loginService.login(username,password);
        if (loginResult.isLogin()){
            log.debug("登录成功");
            return "登录成功";
        } else {
            throw new RuntimeException("登录失败" + loginResult.getResult());
        }
    }
}
